package com.shk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shk.constant.MessageConstant;
import com.shk.entity.Result;
import com.shk.pojo.OrderSetting;
import com.shk.service.OrderSettingService;
import com.shk.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
        @Reference
        private OrderSettingService orderSettingService;
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) throws Exception {
        /*读取上传的文件*/
        List<String[]> list = POIUtils.readExcel(excelFile);
        /*我们需要把传过来文件转换为我们需要的ordersetting类型*/
        if (list != null) {
            List<OrderSetting> arrayList = new ArrayList<>();
            for(String[] strings : list){
                String orderDate = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(orderDate));
                orderSetting.setNumber(Integer.parseInt(number));
                arrayList.add(orderSetting);
            }
            try {
                orderSettingService.add(arrayList);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.UPLOAD_FAIL);
            }
        }
        return new Result(true,MessageConstant.UPLOAD_SUCCESS);
    }

    @RequestMapping("/show")
    public Result show(String date){
        try {
            List<Map> maps = orderSettingService.show(date);
            return new Result(false,MessageConstant.QUERY_ORDER_SUCCESS,maps);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    @RequestMapping("/setnumber")
    private Result setNumber(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.setNumber(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);


        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
